package com.person.sys.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.person.commons.struts2.BaseAction;
import com.person.sys.model.SysFunc;
import com.person.sys.model.SysMenu;
import com.person.sys.model.SysMenuNode;
import com.person.sys.service.SysFuncService;
import com.person.sys.service.SysMenuService;

public class SysFuncAction extends BaseAction {
	
	private SysMenuService sysMenuService;
	private SysFuncService sysFuncService;
	private SysMenu sysMenu;
	private SysFunc sysFunc;
	private List<SysFunc> sysFuncs;
	private List<SysMenuNode> sysMenuNodes;
	private String funcCode;
	private String menuCode;
	private int flag;
	private String menuImage;
	private String nodeName;

	/**
	 * 管理一级节点下的子菜单节点页面
	 * @return
	 */
	public String manageFunc(){
		sysMenu = sysMenuService.findSysMenuById(sysMenu.getMenuId());
		
		sysFunc = sysFuncService.findFuncById(sysFunc.getFuncId());
		
		sysFuncs = sysFuncService.childSysFuncList(sysFunc);
		
		menuImage = sysMenu.getMenuImage();
		
		menuCode = sysMenu.getMenuCode();
		
		return "manageFunc";
	}
	
	
	/**
	 * 转向到添加页面
	 * @return
	 */
	public String preCreate(){
		if(sysMenu != null && StringUtils.isNotBlank(sysMenu.getMenuId())){
			
			sysMenu = sysMenuService.findSysMenuById(sysMenu.getMenuId());
			
			
			menuCode = sysMenu.getMenuCode();
			
			nodeName = sysMenu.getMenuName();
			
		}else if(sysFunc != null && StringUtils.isNotBlank(sysFunc.getFuncId())){
			sysFunc = sysFuncService.findFuncById(sysFunc.getFuncId());
			
			
			menuCode = sysFunc.getMenuCodeGroup();
			
			nodeName = sysFunc.getFuncName();
		}
		
		//sysMenuNodes = sysFuncService.menuSelectList();
		
		return "createFunc";
	}
	
	/**
	 * 添加子级菜单
	 * @return
	 */
	public String create(){
		
		String forward = "";
		
		String menuCodeGroup = menuCode+funcCode;
		
		sysFunc.setMenuCodeGroup(menuCodeGroup);
		
		char status;
		try {
			status = sysFuncService.createSysFunc(sysFunc);
			
			if(status == '0'){
				
				forward = preCreate();
			}else if(status == '1'){
				
				info = getText("func.create.success");//添加成功!
				
				flag = 1;
				List list = sysMenuService.findByCode(menuCode);
				if(list.size() != 0){
					
					sysMenu = sysMenuService.findByCode(menuCode).get(0);
				}
				
				forward = "create_success";
				
			}else if(status == '2'){
				
				info = getText("func.code");//子菜单代码重复2
				forward = preCreate();
			}else if(status == '3'){
				
				info = getText("func.name");//子菜单名称重复3
				forward = preCreate();
			}else if(status == '4'){
				
				info = getText("func.path");//子菜单模块路径重复4
				forward = preCreate();
			}else if(status == '5'){
				
				info = getText("func.nameEn");//子菜单模块路径重复4
				forward = preCreate();
			}else if(status == '6'){
				
				info = getText("func.length");//子菜级数不能超过五级
				forward = preCreate();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			forward = preCreate();
		}
		return forward;
	}
	
	/**
	 * 转向到修改页面
	 * @return
	 */
	public String preModify(){
		//sysMenuNodes = sysFuncService.menuSelectList();
		
		sysFunc = sysFuncService.findFuncById(sysFunc.getFuncId());
		
		String menuCodeGroup = sysFunc.getMenuCodeGroup();
		
		funcCode = menuCodeGroup.substring(menuCodeGroup.length()-2, menuCodeGroup.length());
		menuCode = menuCodeGroup.substring(0, menuCodeGroup.length()-2);
		if(menuCode.length()==2){
			
			sysMenu = sysMenuService.findByMebuCode(menuCode);
			
			nodeName = sysMenu.getMenuName();
		}else{
			
			SysFunc sysFunc1 = sysFuncService.findByFuncCode(menuCode);
			
			nodeName = sysFunc1.getFuncName();
		}
		
		return "modifyFunc";
	}
	
	/**
	 * 修改子级菜单
	 * @return
	 */
	public String modify(){
		String forward = "";
		
		String menuCodeGroup = menuCode+funcCode;
		
		sysFunc.setMenuCodeGroup(menuCodeGroup);
		char status;
		try {
			status = sysFuncService.modifySysFunc(sysFunc);
			
			if(status == '0'){
				
				return preModify();
			}else if(status == '1'){
				
				info = getText("func.create.success");//添加成功!
				
				flag = 1;
				List list = sysMenuService.findByCode(menuCode.substring(0, 2));
				if(list.size() != 0){
					
					sysMenu = (SysMenu)list.get(0);
				}
				
				return "create_success";
				
			}else if(status == '2'){
				
				info = getText("func.code");//子菜单代码重复2
				return preModify();
			}else if(status == '3'){
				
				info = getText("func.name");//子菜单名称重复3
				return preModify();
			}else if(status == '4'){
				
				info = getText("func.path");//子菜单模块路径重复4
				return preModify();
			}else if(status == '5'){
				
				info = getText("func.nameEn");//子菜单模块路径重复4
				forward = preCreate();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			forward = preCreate();
		}
		
		
		return forward;
	}
	
	
	/**
	 * 删除子级菜单
	 * @return
	 */
	public String delete(){
		
		char state;
		try {
			state = sysFuncService.deleteSysFunc(sysFunc);
			
			if(state == '1'){
				info = getText("func.delete.success");
				flag = 1;
			}else{
				info = getText("func.delete.fale");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			info = getText("func.delete.fale");
		}
		
		return "delete_success";
	}

	public SysFunc getSysFunc() {
		return sysFunc;
	}

	public void setSysFunc(SysFunc sysFunc) {
		this.sysFunc = sysFunc;
	}

	public List<SysFunc> getSysFuncs() {
		return sysFuncs;
	}

	public void setSysFuncs(List<SysFunc> sysFuncs) {
		this.sysFuncs = sysFuncs;
	}

	public void setSysFuncService(SysFuncService sysFuncService) {
		this.sysFuncService = sysFuncService;
	}

	public List<SysMenuNode> getSysMenuNodes() {
		return sysMenuNodes;
	}

	public void setSysMenuNodes(List<SysMenuNode> sysMenuNodes) {
		this.sysMenuNodes = sysMenuNodes;
	}

	public String getFuncCode() {
		return funcCode;
	}

	public void setFuncCode(String funcCode) {
		this.funcCode = funcCode;
	}

	public String getMenuCode() {
		return menuCode;
	}

	public void setMenuCode(String menuCode) {
		this.menuCode = menuCode;
	}

	public SysMenu getSysMenu() {
		return sysMenu;
	}

	public void setSysMenu(SysMenu sysMenu) {
		this.sysMenu = sysMenu;
	}

	public void setSysMenuService(SysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}


	public String getMenuImage() {
		return menuImage;
	}


	public void setMenuImage(String menuImage) {
		this.menuImage = menuImage;
	}


	public String getNodeName() {
		return nodeName;
	}


	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	
}
